﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Project_ScansiaPacific
{
    public partial class FrXKChiTiet : DevComponents.DotNetBar.Office2007Form
    {
        public FrXKChiTiet()
        {
            InitializeComponent();
        }
        public void ChungTu(string s)
        {
            lb_ChungTu.Text = s;
        }
        public void Serial(string s)
        {
            lb_Serial.Text = s;
        }
        public void PhanLoaiXuat()
        {
            cb_NguonCungCap.Enabled = true;
            dataGridView1.Columns["TenNguon"].Visible = true;
        }
        private float ChieuDai1, ChieuRong1, ChieuDay1, SoLuong1;
        private double SoKhoi1, TongKhoi1;
        private string NguonCungCap1, Serial1, ChungTu1=null, GhiChu1, child = " - ",AmDo1,TenNguon1;

        private void FrXKChiTiet_Load(object sender, EventArgs e)
        {
            dataGridView1.Columns["SoLuong"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridView1.Columns["SoKhoi"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            combo_load();
        }
        private void combo_load()
        {
            FormProcess Frp = new FormProcess();
            string sql_NCC = @"select MaNCC,TenNCC from dbo.NhaCungCap";
            Frp.cb1(cb_NguonCungCap, sql_NCC, 0, 1);
        }

        private void txt_ChieuDay_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_ChieuDay);
        }

        private void txt_ChieuRong_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_ChieuRong);
        }

        private void txt_ChieuDai_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_ChieuDai);
        }

        private void txt_SoLuong_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_SoLuong);
        }
        private void insert_gridview()
        {
            if (txt_ChieuDay.Text == "" || txt_ChieuRong.Text == "" || txt_ChieuDai.Text == "" || txt_SoLuong.Text == "")
                MessageBox.Show("Nhập thiếu thông tin. Vui lòng kiểm tra lại");
            else
            {
                Process prs = new Process();
                FormProcess Frp = new FormProcess();
                try
                {
                    AmDo1=txt_AmDo.Text;


                    if (cb_NguonCungCap.Text != "")
                    {
                        NguonCungCap1 = prs.substring_left(cb_NguonCungCap.Text, child);
                        TenNguon1 = prs.substring_right(cb_NguonCungCap.Text, child);
                    }
                    else
                    {
                        NguonCungCap1 = cb_NguonCungCap.Text;
                        TenNguon1 = "";
                    }
                    GhiChu1 = txt_GhiChu.Text;
                    if (Frp.isfloat(txt_ChieuDay.Text))
                        ChieuDay1 = float.Parse(txt_ChieuDay.Text);
                    else
                        ChieuDay1 = 0;
                    if (Frp.isfloat(txt_ChieuRong.Text))
                        ChieuRong1 = float.Parse(txt_ChieuRong.Text);
                    else
                        ChieuRong1 = 0;
                    if (Frp.isfloat(txt_ChieuDai.Text))
                        ChieuDai1 = float.Parse(txt_ChieuDai.Text);
                    else
                        ChieuDai1 = 0;
                    if (Frp.isfloat(txt_SoLuong.Text))
                        SoLuong1 = float.Parse(txt_SoLuong.Text);
                    else
                        SoLuong1 = 0;
                    SoKhoi1 = (ChieuRong1 * ChieuDai1 * ChieuDay1 * SoLuong1) / 1000000000;
                    dataGridView1.Rows.Add(ChieuDay1, ChieuRong1, ChieuDai1, SoLuong1, SoKhoi1, NguonCungCap1,TenNguon1, AmDo1, GhiChu1);
                    TongKhoi1 = double.Parse(lb_TongKhoi.Text);
                    TongKhoi1 += SoKhoi1;
                    lb_TongKhoi.Text = TongKhoi1.ToString();
                    txt_SoLuong.Text = "";
                    txt_ChieuDay.Text= "";
                    txt_ChieuRong.Text = "";
                    txt_ChieuDai.Text = "";
                    txt_GhiChu.Text = "";
                    txt_AmDo.Text = "";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

        }

        private void bt_insert_Click(object sender, EventArgs e)
        {
            try
            {
                insert_gridview();
                bt_save.Enabled = true;
                int j = dataGridView1.Rows.Count - 1;
                dataGridView1.CurrentCell = dataGridView1.Rows[j].Cells["ChieuRong"];
                dataGridView1.FirstDisplayedCell = dataGridView1.Rows[j].Cells["ChieuRong"];
                txt_ChieuDay.Focus();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 1)
            {
                try
                {
                    int r = dataGridView1.CurrentCell.RowIndex;
                    TongKhoi1 = double.Parse(lb_TongKhoi.Text) - double.Parse(dataGridView1.Rows[r].Cells["SoKhoi"].Value.ToString());
                    lb_TongKhoi.Text = string.Format("{0:###,###.#########}", TongKhoi1);
                    dataGridView1.Rows.RemoveAt(r);
                    int j = dataGridView1.Rows.Count - 1;
                    dataGridView1.CurrentCell = dataGridView1.Rows[j].Cells["ChieuRong"];
                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
            if (dataGridView1.Rows.Count == 1)
                lb_TongKhoi.Text = "0";
        }

        private void bt_delete_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = dataGridView1.Rows.Count - 2; i >= 0; i--)
                {
                    dataGridView1.Rows.RemoveAt(i);
                }
                lb_TongKhoi.Text = "0";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }     
        }
        private void Save()
        {
            int i;
            Data dta = new Data();
            SqlConnection Conn = dta.Connect();
            SqlCommand cmd = new SqlCommand();
            try
            {
                Conn.Open();
                cmd.Connection = Conn;
                for (i = dataGridView1.Rows.Count - 2; i >= 0; i--)
                {
                    ChieuRong1 = float.Parse(dataGridView1.Rows[i].Cells["ChieuRong"].Value.ToString());
                    ChieuDai1 = float.Parse(dataGridView1.Rows[i].Cells["ChieuDai"].Value.ToString());
                    ChieuDay1 = float.Parse(dataGridView1.Rows[i].Cells["ChieuDay"].Value.ToString());
                    SoLuong1 = float.Parse(dataGridView1.Rows[i].Cells["SoLuong"].Value.ToString());
                    //MessageBox.Show("asdasdas");
                    SoKhoi1 = double.Parse(dataGridView1.Rows[i].Cells["SoKhoi"].Value.ToString());
                    Serial1 = lb_Serial.Text;
                    ChungTu1 = lb_ChungTu.Text;
                    NguonCungCap1 = dataGridView1.Rows[i].Cells["NguonCungCap"].Value.ToString();
                    TenNguon1 = dataGridView1.Rows[i].Cells["TenNguon"].Value.ToString();
                    GhiChu1 = dataGridView1.Rows[i].Cells["GhiChu"].Value.ToString();
                    AmDo1 = dataGridView1.Rows[i].Cells["AmDo"].Value.ToString();
                    //ChungTu1 = txt_ChungTu.Text;
                    string insert_XK = @"insert into dbo.XuatKhoGo(Serial_XK,ChungTu,NguonCungCap,ChieuDay,ChieuRong,ChieuDai,SoKhoi,SoLuong,AmDo,GhiChu,TenNguon) values(";
                    insert_XK += Serial1 + ",N'" + ChungTu1 + "',N'" + NguonCungCap1 + "'," + ChieuDay1 + "," + ChieuRong1 + "," + ChieuDai1 + "," + SoKhoi1 + "," + SoLuong1 + ",N'" + AmDo1 + "',N'" + GhiChu1 + "',N'" +TenNguon1+ "')";

                    cmd.CommandText = insert_XK;
                    cmd.ExecuteNonQuery();
                    dataGridView1.Rows.RemoveAt(i);
                    //Conn.Close();
                }
                MessageBox.Show("Dữ liệu được lưu thành công");
            }
            catch (Exception ex)
            {
                try
                {
                    //Conn.Open();
                    cmd.Connection = Conn;
                    string delete_Serial = @"delete from dbo.Serial_NKGo where Serial_NK=" + Serial1;
                    cmd.CommandText = delete_Serial;
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Chứng từ này sẽ không insert vào DataBase do lỗi nhập liệu \n" + ex.Message);
                    //Conn.Close();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
                this.Close();
            }
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void FrXKChiTiet_KeyDown(object sender, KeyEventArgs ex)
        {
            if (ex.KeyData == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
            if (ex.KeyCode == Keys.F1)
                bt_insert_Click(null, null);
            if (ex.KeyCode == Keys.F11)
                Save();
        }
        private void SLTonKho()
        {
            /*string sql_select = @"select qry1.NhapKhoGo-isnull(qry2.XuatKhoGo,0) as SLTon
                                        from (SELECT     MaHang, SUM(SoLuong) AS HangNhap
                                        FROM         dbo.HangHoa_Nhap
                                        GROUP BY MaHang) as qry1 left join (SELECT     MaHang, SUM(SoLuong) AS HangXuat
                                        FROM         dbo.Hanghoa_Xuat
                                        GROUP BY dbo.Hanghoa_Xuat.MaHang) as qry2
                                        on qry1.MaHang=qry2.MaHang
                                        where qry1.MaHang ='";*/
        }
    }
}
